
<template>
  <el-form>
    <!-- <el-form-item :label-width="formLabelWidth" label="收款单位确认: ">
    </el-form-item> -->

    <el-form-item :label-width="formLabelWidth" label="确认结果: ">
      <el-select
        v-model="Form.isRight.value"
        @change="isWrongChange"
        placeholder="确认结果: "
      >
        <el-option label="--请选择--" value=""></el-option>
        <el-option label="同意" value="60050001"></el-option>
        <el-option label="不同意" value="60050002"></el-option>
      </el-select>
    </el-form-item>

    <el-form-item
      v-show="Form.audComment.show"
      :label-width="formLabelWidth"
      label="确认意见: "
    >
      <el-input
        type="textarea"
        v-model="Form.audComment.value"
        placeholder="确认意见: "
      ></el-input>
    </el-form-item>

    <el-form-item class="btnDir">
      <el-button @click="onSubmit">保 存</el-button>
      <el-button @click="onCancel">取 消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import requestUtil from "@/utils/requestEnc";
import CustOrderService from "@/api/order/CustOrderService";
import { mapState } from "vuex";
export default {
  components: {},
  computed: {
    ...mapState("orders", ["orderItemList"]),
    ...mapState("user", ["user"]),
  },
  async mounted() {
     if(this.CTOInfo instanceof Array){
      this.newCTOInfo = this.CTOInfo;
    }else{
      this.newCTOInfo = [this.CTOInfo];
    }
    if (this.newCTOInfo.length > 0) {
      var orderItemIds = "";

      for (var i = 0; i < this.newCTOInfo.length; i++) {
        if (i == 0) {
          orderItemIds += this.newCTOInfo[i].orderItemId;
        } else {
          orderItemIds += "," + this.newCTOInfo[i].orderItemId;
        }
      }

      this.orderItemIds = orderItemIds;
      


      var scopeInfos = [];
      scopeInfos.push({ scope: "orderTasks" });
      var custSoNumber = this.newCTOInfo[0].custSoNumber;
      var batchNbr = this.newCTOInfo[0].batchNbr;
      var custId = this.newCTOInfo[0].custId;



       let usersMsg = {
        acceptUserId: this.user.staffId,
        acceptUserOrg: this.user.orgId,
        systemUserId: this.user.sysUserId,
        dealType: 234,
        orderItemId:this.newCTOInfo[0].orderItemId,
        orderAuditId:this.newCTOInfo[0].orderAuditId
      };

      const result = await CustOrderService.queryOrderItemDealTask(usersMsg);

      
       var  orderTasks = result.meta.orderItemDealTasks;
      
      var orderTask = orderTasks.filter((item) => {return item.taskId == "TACHE10550001" || item.taskId == "TACHE10570001"});
     
     
      this.orderTask = orderTask[0];
      

      //等传值
      var orderSourceId = "";
      var prodOfferId = "";
      // var taskId = orderTask.TaskId;
      // orderTaskId = orderTask.orderTaskId;

      
    }
  },
  watch: {},
  props: {
    ODKInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
    CTOInfo: {
      type: [Array, Object],
      default: () => {
        return [];
      },
    },
  },
  data() {
    return {
      newCTOInfo:[],
      orderTask: {},
      orderItemIds: "",
      orderItemSeqs: "",
      formLabelWidth: "120px",
      Form: {
        audResultFlag: {
          options: [],
          value: "",
        },
        audComment: {
          value: "",
          show: true,
        },
        isRight: {
          value: "",
        },
        isWrong: {
          value: "",
        },
        wrongReason: {
          value: "-1",
          show: false,
        },
      },
    };
  },
  methods: {
    isNull(val) {
      if (
        val == "NaN" ||
        val == "null" ||
        val == "undefined" ||
        val == undefined ||
        val == ""
      ) {
        return true;
      } else {
        return false;
      }
    },
    isWrongChange(e) {
      if ("60050002" == e) {
        this.Form.audComment.show = true;
      } else {
        this.Form.audComment.show = false;
        this.Form.wrongReason.value = "";
      }
    },

    //表单提交按钮事件
    async onSubmit() {
  
      if (this.isNull(this.Form.isRight.value)) {
        this.$message({
          showClose: true,
          message: "确认结果不能为空",
          type: "error",
        });
        return;
      }

      if (this.Form.isRight.value == 60050002) {
        if (this.isNull(this.Form.audComment.value)) {
          this.$message({
            showClose: true,
            message: "意见不能为空",
            type: "error",
          });
          return;
        }
      }

      var orderTasks = [];

       
      var custId = this.newCTOInfo[0].custId;
      //内层orderTask表更新需要的数据
      for (var i = 0; i < this.newCTOInfo.length; i++) {
        orderTasks.push({
          actType: "MOD",
          orderTaskId: this.orderTask.orderTaskId,
          orderItemId: this.newCTOInfo[i].orderItemId,
          //taskId: this.orderTask.taskId,
          taskId: this.orderTask.taskId,
          nodeName: this.orderTask.nodeName,
          result: this.Form.isRight.value,
          taskComment: this.Form.audComment.value,
          custId: this.newCTOInfo[i].custId,
          userId:this.user.staffId
        });
      }
      //外层主参数
      //流程用审核表示，默认不同意为0
      var orderAudFlag = 0;
      if(this.Form.isRight.value==60050001){
          orderAudFlag = 1;
      }
      var param = {
        custSoNumber: this.newCTOInfo[0].custSoNumber,
        orderItemIds: this.orderItemIds,
        orderAuditId: this.newCTOInfo[0].orderAuditId,
        oneOrmany: "one",
        userId: this.user.staffId,
        orderTasks: orderTasks,
        taskId: this.orderTask.taskId,
        custId:custId,
        orderAudFlag:orderAudFlag
      };
      
      
      var result = await CustOrderService.confirmCharge(param);
      
      var meta = result.meta;

      if (!this.isNull(meta) && meta.resultCode == 0) {
        this.$message({
          showClose: true,
          message: "保存成功",
          type: "susess",
        });
        this.onCancel();
      } else {
        this.$message({
          showClose: true,
          // message: meta.resultMsg,
          message: meta.resultMsg,

          type: "error",
        });
      }
    },
    onCancel() {
      this.$emit("close");
    },
  },
};
</script>

<style>
.el-form--inline .el-form-item {
  display: inline-flex;
  margin-right: 0.1rem;
  vertical-align: top;
  width: 42%;
}

.el-form-item__label {
  width: 94px;
}
.btnDir {
  margin-left: 36%;
  margin-top: 19px;
}
</style>